package com.ktgj.service.service.impl;


import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.AirportApi;
import com.ktgj.flight.api.domain.AirportTerminalApi;
import com.ktgj.hotel.api.RemoteHotelService;
import com.ktgj.hotel.api.domain.DicCityApi;
import com.ktgj.service.domain.ContractLinkVipchannel;
import com.ktgj.service.domain.ContractLinkVipchannelPrice;
import com.ktgj.service.domain.Vipchannel;
import com.ktgj.service.domain.VipchannelCommon;
import com.ktgj.service.domain.vo.ChannelVo;
import com.ktgj.service.domain.vo.VipHallVipChannelPrice;
import com.ktgj.service.domain.vo.VipchannelVo;
import com.ktgj.service.mapper.ContractLinkVipchannelMapper;
import com.ktgj.service.mapper.ContractLinkVipchannelPriceMapper;
import com.ktgj.service.mapper.VipchannelMapper;
import com.ktgj.service.service.IVipchannelService;
import com.ktgj.train.api.RemoteTrainService;
import com.ktgj.train.api.domain.TrainTrainstationApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * VIP通道			
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
@Transactional
public class VipchannelServiceImpl implements IVipchannelService 
{
    @Autowired
    private VipchannelMapper vipchannelMapper;
    @Autowired
    private ContractLinkVipchannelPriceMapper contractLinkVipchannelPriceMapper;

    //vip通道合同中间表
    @Autowired
    private ContractLinkVipchannelMapper contractLinkVipchannelMapper;

    @Autowired
    private RemoteFlightService remoteFlightService;

    @Autowired
    private RemoteHotelService remoteHotelService;

    @Autowired
    private RemoteTrainService remoteTrainService;

    /**
     * 查询VIP通道			

     * 
     * @param vipchannelId VIP通道			
ID
     * @return VIP通道			

     */
    @Override
    public Vipchannel selectVipchannelById(Long vipchannelId)
    {
        return vipchannelMapper.selectVipchannelById(vipchannelId);
    }

    /**
     * 查询VIP通道			
列表
     * 
     * @param vipchannel VIP通道			

     * @return VIP通道			

     */
    @Override
    public List<Vipchannel> selectVipchannelList(Vipchannel vipchannel)
    {
        return vipchannelMapper.selectVipchannelList(vipchannel);
    }

    @Override
    public List<Vipchannel> selectVipchannelAndContractList(Long contractId) {
        List<Vipchannel> lstVipChannel = vipchannelMapper.selectVipchannelAndContractList(contractId);
        for (Vipchannel vipchannel: lstVipChannel) {
            Long contractVipChannel = vipchannel.getContractLinkVipchannel().getContractVipchannelid();
            vipchannel.getLstVipchannelPrice().addAll(contractLinkVipchannelPriceMapper.selectVipchannelPriceByContractVipchannelId(contractVipChannel));
        }
        return lstVipChannel;
    }

    /**
     * 新增VIP通道			

     * 
     * @param vipchannel VIP通道			

     * @return 结果
     */
    @Override
    public int insertVipchannel(Vipchannel vipchannel)
    {
        vipchannel.setCreateTime(DateUtils.getNowDate());
        return vipchannelMapper.insertVipchannel(vipchannel);
    }

    /**
     * 修改VIP通道			

     * 
     * @param vipchannel VIP通道			

     * @return 结果
     */
    @Override
    public int updateVipchannel(Vipchannel vipchannel)
    {
        vipchannel.setUpdateTime(DateUtils.getNowDate());
        return vipchannelMapper.updateVipchannel(vipchannel);
    }

    /**
     * 批量删除VIP通道			

     * 
     * @param vipchannelIds 需要删除的VIP通道			
ID
     * @return 结果
     */
    @Override
    public int deleteVipchannelByIds(Long[] vipchannelIds)
    {
        return vipchannelMapper.deleteVipchannelByIds(vipchannelIds);
    }

    /**
     * 删除VIP通道			
信息
     * 
     * @param vipchannelId VIP通道			
ID
     * @return 结果
     */
    @Override
    public int deleteVipchannelById(Long vipchannelId)
    {
        return vipchannelMapper.deleteVipchannelById(vipchannelId);
    }

    /**
     * vip通道价格维护
     * @param vipchannel
     * @return
     */
    @Override
    public boolean addVipChannelPrice(ContractLinkVipchannel vipchannel) {
        boolean flag = false;
        int count = 0;
        //先新增vip通道中间表数据
        vipchannel.setCreateTime(DateUtils.getNowDate());
        int result = contractLinkVipchannelMapper.insertContractLinkVipchannel(vipchannel);
        if(result > 0){
            //获取中间新增数据后的主键值
            Long contractVipchannelid = vipchannel.getContractVipchannelid();
            List<ContractLinkVipchannelPrice> prices = vipchannel.getVipchannelPrices();
            for (ContractLinkVipchannelPrice price : prices) {
                    //封装数据
                ContractLinkVipchannelPrice vipchannelPrice = new ContractLinkVipchannelPrice();
                vipchannelPrice.setContractVipchannelid(contractVipchannelid);
                vipchannelPrice.setDayId(price.getDayId());
                vipchannelPrice.setBasePrice(price.getBasePrice());
                vipchannelPrice.setRecommendedRetailprice(price.getRecommendedRetailprice());
                vipchannelPrice.setSalePrice(price.getSalePrice());
                vipchannelPrice.setStatus(price.getStatus());
                vipchannelPrice.setOpemployeeId(price.getOpemployeeId());
                vipchannelPrice.setUpdateTime(price.getUpdateTime());
                vipchannelPrice.setCreateTime(DateUtils.getNowDate());

                //执行保存操作
                count = contractLinkVipchannelPriceMapper.insertContractLinkVipchannelPrice(vipchannelPrice);
            }
        }
        if(count>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public int selectVipChannelByContractIdAndVipChannelId(Long contractId, Long vipChannelId) {
       return contractLinkVipchannelMapper.selectVipChannelByContractIdAndVipChannelId(contractId,vipChannelId);
    }

    @Override
    public List<Vipchannel> getVipchannelList() {
        return vipchannelMapper.getAll();
    }

    @Override
    public List<Vipchannel> queryVipchannel(Long vipchannelId) {
        List<Vipchannel> vipchannelList = vipchannelMapper.queryVipchannel(vipchannelId);
        return vipchannelList;
    }

    @Override
    public List<Map<String, Object>> queryVipchannelMap(Long vipchannelId) {
        List<Vipchannel> vipchannelList = vipchannelMapper.queryVipchannel(vipchannelId);
        List<Map<String, Object>> mapList = new ArrayList<>();
        StringBuilder builder;
        for (Vipchannel vipchannels : vipchannelList) {
            Map<String, Object> map = new HashMap<>();
            map.put("vipchannelId", vipchannels.getVipchannelId());
            map.put("iconPath", vipchannels.getIconPath());
            map.put("viphallSetOutType", vipchannels.getViphallSetOutType());
            map.put("freeMember", vipchannels.getFreeMember());
            map.put("tagName", vipchannels.getTagName());
            map.put("address", vipchannels.getAddress());
            Long businessBeginHour = vipchannels.getBusinessBeginHour();
            if (businessBeginHour != null) {
                builder = new StringBuilder(businessBeginHour.toString());
                builder.insert(builder.length() - 2, ":");
                map.put("businessBeginHour", builder);
            }

            Long businessEndHour = vipchannels.getBusinessEndHour();
            if (businessEndHour != null) {
                builder = new StringBuilder(businessEndHour.toString());
                builder.insert(builder.length() - 2, ":");
                map.put("businessEndHour", builder);
            }

            for (ContractLinkVipchannelPrice contractLinkVipchannelPrice : vipchannels.getLstVipchannelPrice()) {
                map.put("basePrice", contractLinkVipchannelPrice.getBasePrice());
                map.put("recommendedRetailprice", contractLinkVipchannelPrice.getRecommendedRetailprice());
                map.put("salePrice", contractLinkVipchannelPrice.getSalePrice());
                map.put("accompanyingPrice", contractLinkVipchannelPrice.getAccompanyingPrice());
            }

            Long terminalId = vipchannels.getAirportTerminalId();
            String terminal = remoteFlightService.queryStartTerminal(terminalId);
            map.put("startTerminal", terminal);

            String airportId = vipchannels.getAirportCode();
            String airPortName = remoteFlightService.queryAirPortName(airportId);
            map.put("airPortName", airPortName);
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public Vipchannel queryVipchannelByVipchannelId(Long vipchannelId) {
        return vipchannelMapper.queryVipchannelByVipchannelId(vipchannelId);
    }

    /**
     * 根据机场三字码查询
     * */
    @Override
    public boolean selectVipchannelByAirportCode(String airportCode) {
        List<Vipchannel> vipchannelList = vipchannelMapper.selectVipchannelByAirportCode(airportCode);
        boolean flag = false;
            if (vipchannelList.size() != 0){
                flag = true;
            }
        return flag;
    }

    @Override
    public List<VipchannelCommon> commonServices(Long customerId) {
        List<VipchannelCommon> vipchannelCommons = vipchannelMapper.commonServices(customerId);
        StringBuilder builder;
        for (VipchannelCommon vipchannelCommon : vipchannelCommons) {
            String businessBeginHour = vipchannelCommon.getBusinessBeginHour();
            if (businessBeginHour != null) {
                builder = new StringBuilder(businessBeginHour);
                builder.insert(builder.length() - 2, ":");
                vipchannelCommon.setBusinessBeginHour(builder.toString());
            }
            String businessEndHour = vipchannelCommon.getBusinessEndHour();
            if (businessEndHour != null) {
                builder = new StringBuilder(businessEndHour);
                builder.insert(builder.length() - 2, ":");
                vipchannelCommon.setBusinessEndHour(builder.toString());
            }
            String vipchannelType = "vipchannel";
            vipchannelCommon.setType(vipchannelType);

            Long terminalId = vipchannelCommon.getAirportTerminalId();
            String terminal = remoteFlightService.queryStartTerminal(terminalId);
            vipchannelCommon.setStartTerminal(terminal);

            String score = "4.3";
            vipchannelCommon.setScore(score);
        }
        return vipchannelCommons;
    }

    @Override
    public VipHallVipChannelPrice selectVipChannelPrice(Long id) {
        VipHallVipChannelPrice price = new VipHallVipChannelPrice();
        ContractLinkVipchannelPrice channelPrice = vipchannelMapper.selectVipChannelPrice(id);
        price.setSalePrice(channelPrice.getSalePrice());
        price.setRecommendedRetailPrice(channelPrice.getRecommendedRetailprice());
        price.setBasePrice(channelPrice.getBasePrice());
        return price;
    }

    @Override
    public int queryVipChannelNumber(Long customerId) {
        return vipchannelMapper.queryVipChannelNumber(customerId);
    }

    /**
     * vip通道基本数据列表
     * */
    @Override
    public List<Vipchannel> selectVipchannelList(ChannelVo channelVo) throws Exception {
        /*if (channelVo.getCity() != null && !channelVo.getCity().equals("")){
            Map<String, Object> map = new HashMap<>();
            map.put("code", channelVo.getCityId());
            TrainTrainstationApi trainTrainstationApi = remoteTrainService.selectCityName(map);
            channelVo.setCity(trainTrainstationApi.getAirportcityname());
        }*/
        List<Vipchannel> vipchannels = vipchannelMapper.selectVipchannelBasic(channelVo);
        if (vipchannels.size() > 0){
            for (Vipchannel vipchannel : vipchannels) {
                if (vipchannel.getBusinessBeginHour() != null && vipchannel.getBusinessBeginHour() != 0)
                {
                    String businessBeginHour = vipchannel.getBusinessBeginHour().toString();
                    String time = businessBeginHour.substring(0, businessBeginHour.length() - 2) + ":" + businessBeginHour.substring(businessBeginHour.length() - 2);
                    vipchannel.setTime(time);
                    String endHour = vipchannel.getBusinessEndHour().toString();
                    if (businessBeginHour.length() == 3)
                    {
                        vipchannel.setBeginTime("0" + businessBeginHour.substring(0, 1) + ":" + businessBeginHour.substring(businessBeginHour.length() - 2));
                    } else
                    {
                        vipchannel.setEndTime(endHour.substring(0, endHour.length() - 2) + ":" + endHour.substring(endHour.length() - 2));
                    }
                }

                Map<String , Object> airportId = new HashMap<>();
                airportId.put("airportId", vipchannel.getAirportCode());
                AirportApi airportApi = remoteFlightService.getAirportInfoByAirportName(airportId);
                if (airportApi != null){
                    vipchannel.setAirportName(airportApi.getAirportName());
                }

                Map<String, Object> id = new HashMap<>();
                id.put("id", vipchannel.getCityId());
                DicCityApi dicCityApi = remoteHotelService.selectCityName(id);
                if (dicCityApi != null){
                    vipchannel.setCityName(dicCityApi.getCityName());
                }

                Map<String, Object> airportTerminalId = new HashMap<>();
                airportTerminalId.put("id", vipchannel.getAirportTerminalId());
                AirportTerminalApi infoById = remoteFlightService.getInfoById(airportTerminalId);
                if (infoById != null){
                    vipchannel.setTerminalName(infoById.getStartTerminal());
                }

                if (vipchannel.getVipchannelLocationTypeId() != null){
                    if (vipchannel.getVipchannelLocationTypeId().equals("1")){
                        vipchannel.setVipchannelLocationTypeId("国内机场");
                    }
                    else if (vipchannel.getVipchannelLocationTypeId().equals("2")){
                        vipchannel.setVipchannelLocationTypeId("高铁");
                    }
                    else if (vipchannel.getVipchannelLocationTypeId().equals("3")){
                        vipchannel.setVipchannelLocationTypeId("国际机场");
                    }
                    else if (vipchannel.getVipchannelLocationTypeId().equals("4")){
                        vipchannel.setVipchannelLocationTypeId("火车站");
                    }
                    else if (vipchannel.getVipchannelLocationTypeId().equals("5")){
                        vipchannel.setVipchannelLocationTypeId("港口");
                    }
                }
            }
            return vipchannels;
        }
        return null;
    }

    /**
     * 添加VIP通道信息
     * */
    @Override
    public int addVipchannel(Vipchannel vipchannel) {
        Date date = new Date();
        vipchannel.setIsdeleted("false");
        vipchannel.setCreateTime(date);
        vipchannel.setUpdateTime(date);
        int i;
        if (vipchannel.getVipchannelId() != null && vipchannel.getVipchannelId() != 0){
            i = vipchannelMapper.updateVipchannel(vipchannel);
        }
        else {
            i = vipchannelMapper.insertVipchannel(vipchannel);
        }
        return i;
    }

    /**
     * 删除vip通道信息
     * */
    @Override
    public int delVipchannel(Long id) {
        int i = vipchannelMapper.delVipchannel(id);
        return i;
    }
}
